package com.mashuai.utils.ztest.toolkit.excel;

import com.mashuai.utils.base.MsAssertUtils;
import com.mashuai.utils.toolkit.core.msExcel.MsExcelInvoke;
import com.mashuai.utils.toolkit.core.msExcel.dto.MsDynamicExportDto;
import com.mashuai.utils.toolkit.core.msExcel.dto.MsImportDto;
import com.mashuai.utils.toolkit.core.msExcel.dto.MsTemplateExportDto;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;

/**
 * @ClassName: ExcelController
 * @Description:
 */
@RestController
@RequestMapping("/test/excel")
public class ExcelController {

    @PostMapping("/dynamicExport")
    public void dynamicExport(HttpServletResponse response, @RequestBody MsDynamicExportDto dto) throws Exception {
        MsExcelInvoke.dynamicExport(response, dto);
    }


    @PostMapping("/templateExport")
    public void templateExport(HttpServletResponse response, @RequestBody MsTemplateExportDto dto) throws Exception {
        MsExcelInvoke.templateExport(response, dto);
    }

    @PostMapping("/import")
    public Object readExcel(@RequestParam("static/template") MultipartFile file,
                            @RequestParam("targetParam") String targetParam,
                            @RequestParam("headRow") Integer headRow,
                            @RequestParam(value = "remark", defaultValue = "") String remark) {
        MsImportDto dto = new MsImportDto(targetParam, headRow, remark);
        Object result = null;
        try {
            result = MsExcelInvoke.importExcel(file, dto);
        } catch (Exception e) {
            MsAssertUtils.throwInnerException(e.getMessage());
        }
        return result;
    }

}
